const Service = require("egg").Service;

class VideoService extends Service {
  // 获取视频列表
  async getVideoList(query) {
    const { app } = this;
    try {
      // 查询Video表中的总数量
      let totalNum = await app.model.Video.count({
        where: { delFlag: 0 },
      });
      let videoList = await app.model.Video.findAll({
        attributes: ["id", "title", "content", "thumbnail", "video_url"],
        include: {
          model: app.model.User,
          attributes: ["id", "userName", "nickName", "headPicture"],
        },
        where: { delFlag: 0 },
        offset: (query.currentPage - 1) * query.pageSize,
        limit: parseInt(query.pageSize),
      });
      return {
        videoList,
        totalNum,
        totalPages: Math.ceil(totalNum / query.pageSize),
      };
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  // 保存视频
  async saveVideo({ id, title, content, thumbnail, video_url, user_id }) {
    const { app } = this;
    try {
      if (!id) {
        return await app.model.Video.create({
          title,
          content,
          thumbnail,
          video_url,
          user_id,
        });
      } else {
        const exit = await app.model.Video.findByPk(id);
        if (!exit) {
          this.ctx.logger.debug(`根据${id}无法查询到模板`);
          throw new Error("无法查询到模板");
        }
        exit.update({ title, content, user_id });
        return true;
      }
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //查询视频
  async findVideoById(id) {
    const { app } = this;
    try {
      return await app.model.Video.findOne({
        where: { id },
      });
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //修改视频
  async modifyVideo({ id, title, content, thumbnail, video_url, user_id }) {
    const { app } = this;
    try {
      if (!id) {
        console.log(
          "🚀 ~ file: video.js:76 ~ VideoService ~ modifyVideo ~ id",
          id
        );
        return await app.model.Video.create({
          title,
          content,
          thumbnail,
          video_url,
          user_id,
          created_time: new Date(),
          updated_time: new Date(),
        });
      } else {
        const exit = await app.model.Video.findByPk(id);
        if (!exit) {
          this.ctx.logger.debug(`根据${id}无法查询到模板`);
          throw new Error("无法查询到模板");
        }
        exit.update({
          title,
          content,
          thumbnail,
          video_url,
          user_id,
          updated_time: new Date(),
        });
        return true;
      }
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //删除视频
  async deleteVideo(id) {
    const { app } = this;
    try {
      const exit = await app.model.Video.findByPk(id);
      if (!exit) {
        this.ctx.logger.debug(`根据${id}无法查询到模板`);
        throw new Error("无法查询到模板");
      }
      exit.update({ delFlag: 1 });
      return true;
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }
}

module.exports = VideoService;
